<template>
  <div class="different-faces">
    <div id="scene" @mousemove="onMouseMove"></div>
  </div>
</template>

<script>
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";

import { EffectComposer } from "three/examples/jsm/postprocessing/EffectComposer.js";
import { RenderPass } from "three/examples/jsm/postprocessing/RenderPass.js";
import { ShaderPass } from "three/examples/jsm/postprocessing/ShaderPass.js";
import { OutlinePass } from "three/examples/jsm/postprocessing/OutlinePass.js";
import { FXAAShader } from "three/examples/jsm/shaders/FXAAShader.js";

let scene;
let camera;
let renderer;
let controls;

let raycaster;
let mouse;

let outlinePass;
let composer;
export default {
  data() {
    return {
      cube: null,
      tube: null,
    };
  },
  mounted() {
    this.init();
  },
  computed: {
    windowWidth() {
      return window.innerWidth - 200;
    },
  },
  methods: {
    onMouseMove(event) {
      // 将鼠标位置归一化为设备坐标。x 和 y 方向的取值范围是 (-1 to +1)
      mouse.x = ((event.clientX - 200) / this.windowWidth) * 2 - 1;
      mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
      //射线只检查指定的成员：[方块]
      const intersects = raycaster.intersectObjects([this.cube, this.sphere]);
      if (intersects.length > 0) {
        outlinePass.selectedObjects = [intersects[0].object];
        // console.log(intersects[0].object.name);
      } else {
        outlinePass.selectedObjects = [];
      }
    },
    // 初始化
    init() {
      this.createScene(); // 创建场景
      this.createCube(); // 创建方块
      this.createLight(); // 创建光源
      this.createCamera(); // 创建相机
      this.createRender(); // 创建渲染器
      this.createControls(); // 创建控件对象
      this.postprocessing();

      this.render(); // 渲染
    },
    // 创建场景
    createScene() {
      scene = new THREE.Scene();
      //用于简单模拟3个坐标轴的对象.
      //红色代表 X 轴. 绿色代表 Y 轴. 蓝色代表 Z 轴.
      scene.add(new THREE.AxesHelper(20));

      raycaster = new THREE.Raycaster();
      mouse = new THREE.Vector2();
    },
    // 创建方块
    createCube() {
      const cubeGeom = new THREE.BoxGeometry(4, 4, 4);
      this.cube = new THREE.Mesh(
        cubeGeom,
        new THREE.MeshPhongMaterial({ color: 0x0000aa })
      );
      this.cube.name = "test立方体";
      scene.add(this.cube);

      const sphereGeom = new THREE.SphereGeometry(3);
      this.sphere = new THREE.Mesh(sphereGeom, new THREE.MeshPhongMaterial());

      this.sphere.position.set(5, 0, 0);
      this.sphere.name = "test球体";
      scene.add(this.sphere);
    },

    // 创建光源
    createLight() {
      // 环境光
      const ambientLight = new THREE.AmbientLight(0xffffff, 0.1); // 创建环境光
      scene.add(ambientLight); // 将环境光添加到场景

      const spotLight = new THREE.SpotLight(0xffffff); // 创建聚光灯
      spotLight.position.set(-20, 40, -10);
      spotLight.castShadow = true;
      scene.add(spotLight);
    },
    // 创建相机
    createCamera() {
      const element = document.getElementById("scene");
      const k = element.clientWidth / element.clientHeight; // 窗口宽高比
      // PerspectiveCamera( fov, aspect, near, far )
      camera = new THREE.PerspectiveCamera(35, k, 0.1, 1000);
      camera.position.set(-10, 10, 20); // 设置相机位置

      camera.lookAt(new THREE.Vector3(10, 0, 0)); // 设置相机方向
      scene.add(camera);
    },
    // 创建渲染器
    createRender() {
      const element = document.getElementById("scene");
      renderer = new THREE.WebGLRenderer();
      renderer.setSize(element.clientWidth, element.clientHeight); // 设置渲染区域尺寸
      renderer.shadowMap.enabled = true; // 显示阴影
      renderer.shadowMap.type = THREE.PCFSoftShadowMap;
      renderer.setClearColor(0x3f3f3f, 1); // 设置背景颜色
      element.appendChild(renderer.domElement);
    },
    render() {
      // 通过摄像机和鼠标位置更新射线
      raycaster.setFromCamera(mouse, camera);

      renderer.render(scene, camera);
      requestAnimationFrame(this.render);
      composer.render();
    },
    // 创建控件对象
    createControls() {
      controls = new OrbitControls(camera, renderer.domElement);
    },
    postprocessing() {
      composer = new EffectComposer(renderer);

      const renderPass = new RenderPass(scene, camera);
      composer.addPass(renderPass);

      outlinePass = new OutlinePass(
        new THREE.Vector2(window.innerWidth, window.innerHeight),
        scene,
        camera
      );
      composer.addPass(outlinePass);

      const effectFXAA = new ShaderPass(FXAAShader);
      effectFXAA.uniforms["resolution"].value.set(
        1 / window.innerWidth,
        1 / window.innerHeight
      );
      composer.addPass(effectFXAA);

      outlinePass.visibleEdgeColor.set(0x00ff00); // 边缘可见部分发颜色 sColor[0].color
      // outlinePass.hiddenEdgeColor.set(outlineColor[1]); // 边缘遮挡部分发光颜色 sColor[1].color
      // outlinePass.edgeStrength = Number(10.0); //边框的亮度
      // outlinePass.edgeGlow = Number(1); //光晕[0,1]
      // outlinePass.edgeThickness = Number(1.0); //边缘浓度
      // outlinePass.pulsePeriod = Number(1.8); //呼吸闪烁的速度 闪烁频率 ，默认0 ，值越大频率越低
      // outlinePass.usePatternTexture = false; //是否使用父级的材质
      // outlinePass.downSampleRatio = 2; // 边框弯曲度
      // outlinePass.clear = true;
    },
  },
};
</script>
<style>
.different-faces {
  width: 100%;
  height: 100%;
  position: relative;
}
#scene {
  width: 100%;
  height: 100%;
}
.controls-box {
  position: absolute;
  right: 5px;
  top: 5px;
  width: 300px;
  padding: 10px;
  background-color: #fff;
  border: 1px solid #c3c3c3;
}
.vertice-span {
  line-height: 38px;
  padding: 0 2px 0 10px;
}
</style>

